const fs = require('fs');
const path = require('path');
const chalk = require('chalk');

function genInstallAll(filePaths, targetFilePath) {
  if (typeof filePaths !== 'string' && !Array.isArray(filePaths)) {
    console.log(chalk.red(`${filePaths} 必须是或者字符串`));
    return;
  }

  targetFilePath = path.resolve(__dirname, targetFilePath);

  try {
    fs.statSync(targetFilePath);
    fs.unlinkSync(targetFilePath);
  } catch (error) {
  }

  if (!Array.isArray(filePaths)) {
    filePaths = [filePaths];
  }

  filePaths.forEach(filePath => {
    filePath = path.resolve(__dirname, filePath);
    try {
      const stat = fs.statSync(filePath);
      if (stat.isFile()) {
        const content = fs.readFileSync(filePath, 'utf-8');
        fs.appendFileSync(targetFilePath, content + '\n\n');
      }
    } catch (error) {
      console.log(chalk.red('error', filePath));
    }
  });

  const endContent = 'source ~/.bashrc'
  fs.appendFileSync(targetFilePath, endContent + '\n\n');
}

genInstallAll(
  [
    '../tasks/install-init.sh',
    '../tasks/install-anaconda.sh',
    '../tasks/install-java.sh',
    '../tasks/install-jenkins.sh',
    '../tasks/install-mongodb.sh',
    '../tasks/install-mysql.sh',
    '../tasks/install-nginx.sh',
    '../tasks/install-nvm.sh',
    '../tasks/install-redis.sh',
  ],
  '../tasks/install-all.sh'
);
